package com.qs.eggs.view.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.adapter.SubEvaluationdapter;
import com.qs.eggs.base.activity.BaseActivity;
import com.qs.eggs.bean.CollectBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.w3c.dom.Text;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class EvaluationActivity extends BaseActivity {
    private RecyclerView recyclerView;
    private TextView mSubmit;
    private String orderid;
    private ArrayList<HashMap<String,String>> mInfo;
    private SubEvaluationdapter subEvaluationdapter;
    @Override
    protected int getContentView() {
        return R.layout.activity_evaluation;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        orderid=getIntent().getStringExtra("orderid");
        mSubmit=(TextView)findViewById(R.id.tv_submit);
        recyclerView=(RecyclerView)findViewById (R.id.recycler);
        mSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mInfo=subEvaluationdapter.getInfo();
                HashMap<String,Object> map=new HashMap<String, Object>();
                map.put("evaluteList",mInfo);
                map.put("orderid",orderid);
                Gson gson=new Gson();
                String params=gson.toJson(map);
                OkGo.post(Constants.URL_BASE+"evaulteManage?action=addEvaulte")
                        .params("data",params)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                if(s.equals("\"true\"")){
                                    Toast.makeText(EvaluationActivity.this,"评价成功",Toast.LENGTH_SHORT).show();
                                    finish();
                                }
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                super.onError(call, response, e);
                            }
                        });

            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        HashMap<String,String> map=new HashMap<>();
        map.put("orderid",orderid);
        Gson gson=new Gson();
        String params=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"myorder?action=searchMyOrderDetail")
                .params("data",params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        Type type = new TypeToken<ArrayList<CollectBean>>() {
                        }.getType();

                        GsonBuilder gsonBuilder = new GsonBuilder();
                        ArrayList<CollectBean> resultBean = gsonBuilder.create().fromJson(s, type);

                        subEvaluationdapter=new SubEvaluationdapter(EvaluationActivity.this,resultBean);
                        recyclerView.setAdapter(subEvaluationdapter);
                        recyclerView.setLayoutManager(new LinearLayoutManager(EvaluationActivity.this));
                        recyclerView.setItemAnimator(new DefaultItemAnimator());


                    }
                });

    }
}
